<?php
namespace Wx\Controller;
use Think\Controller;


class MyEvaluateController extends CommonController {

    // 评价列表
    public function index(){
        // 获取该商品所有的评价
        $sg_id = intval(I('sg_id'));
        $count = M('evaluate')->where('sg_id = '.$sg_id)->count();    //评价总数
        $list = M('evaluate')->table(C('DB_PREFIX')."evaluate as e")  //评价详情
                ->join(C('DB_PREFIX')."member as m ON m.id=e.u_id")
                ->field('e.*,m.nickname,m.avatar,m.hometown')
                ->where("sg_id=".$sg_id)
                ->order('id desc')
                ->select();
        foreach ($list as $k => $v){
            $list[$k]['comm_num'] = M('evaluate_comment')->where('e_id = '.$v['id'])->count();  //评论数
            $list[$k]['pic'] = $var=explode(",",$v['pic']);              //处理评价图片
        }
//                dump($list);
        $this->assign('count',$count);
        $this->assign('list',$list);
        $this->display('evaluatelist');
    }// end fun index()

    // 商品评价详情
    public function commentdetail(){
        $e_id = intval(I('e_id'));
        $e_info = M('evaluate')->where('id = '.$e_id)->find();  //评价详情
        $m_info = M('member')->where('id = '.$e_info['u_id'])->field('nickname,avatar,hometown')->find();      //评价用户详情
        $e_info['pic'] = $var=explode(",",$e_info['pic']);         //处理评价图片
        $comment = M('evaluate_comment')->table(C('DB_PREFIX')."evaluate_comment as c")  //评价-评论详情
                    ->join(C('DB_PREFIX')."member as m ON m.id=c.send_id")
                    ->field('c.*,m.nickname,m.avatar,m.hometown')
                    ->where('e_id = '.$e_id)
                    ->order('id desc')
                    ->select();
        $count = M('evaluate_comment')->where('e_id = '.$e_id)->count();    //评论总数
        $this->assign('e_info',$e_info);
        $this->assign('m_info',$m_info);
        $this->assign('comment',$comment);
        $this->assign('count',$count);
        $this->display();
    }// end fun commentdetail()

    // 添加评价de评价
    public function addComment(){
        if(IS_POST){
            $post = I('post.');
            $post['send_id'] = session('u_id'); //评论发送人id
            $post['addtime'] = time();
            $post['accept_id'] = M('evaluate')->where('id = '.$post['e_id'])->getField('u_id'); //评论接收人id
            $res = M('evaluate_comment')->add($post);
            if($res){
                $this->success('提交成功');
            }
        }
    }// end fun addComment()


    // 点赞
    public function addLike(){
        if(IS_POST){
            $e_id = intval(I('e_id'));
            $table = I('table');
            M($table)->where('id = '.$e_id)->setInc('like_num',1);
        }
    }// end fun addLike()

    // 取消点赞
    public function cancleLike(){
        if(IS_POST){
            $e_id = intval(I('e_id'));
            $table = I('table');
            M($table)->where('id = '.$e_id)->setDec('like_num',1);
        }
    }// end fun cancleLike()

}//end class